home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / lib / tclX6.4c / dist / tests / chmod.test < prev    next >
Encoding:
Text File  |  1992-11-07  |  10.6 KB  |  394 lines

  1. #
  2. # chmod.test
  3. #
  4. # Tests for the chmod, chown and chgrp commands.
  5. #---------------------------------------------------------------------------
  6. # Copyright 1992 Karl Lehenbauer and Mark Diekhans.
  7. #
  8. # Permission to use, copy, modify, and distribute this software and its
  9. # documentation for any purpose and without fee is hereby granted, provided
  10. # that the above copyright notice appear in all copies.  Karl Lehenbauer and
  11. # Mark Diekhans make no representations about the suitability of this
  12. # software for any purpose.  It is provided "as is" without express or
  13. # implied warranty.
  14. #------------------------------------------------------------------------------
  15. # $Id: chmod.test,v 2.0 1992/10/16 04:49:26 markd Rel $
  16. #------------------------------------------------------------------------------
  17. #
  18.  
  19. if {[info procs test] != "test"} then {source testlib.tcl}
  20.  
  21. #-----------------------------------------------------------------------------
  22. # This routine to the the mode of a file.  It is returned formated in octal.
  23.  
  24. proc GetMode {filename} {
  25.     file stat $filename stat
  26.     return [format "%o" [expr {$stat(mode) & 07777}]]
  27. }
  28.  
  29. #-----------------------------------------------------------------------------
  30. # Certain Unix systems don't handle chmod the same.  This routine test if the
  31. # system chmod produces the expected results.
  32. #   o mode - symbolic mode to set the file to.
  33. #   o expect - expected result from ls.
  34. #
  35. proc CheckChmod {mode expect} {
  36.     chmod 000 CHECK.TMP
  37.     exec chmod $mode CHECK.TMP
  38.     set sysMode [lindex [exec ls -l CHECK.TMP] 0]
  39.     return [expr {"$sysMode" == "$expect"}]
  40. }
  41.  
  42.  
  43. #-----------------------------------------------------------------------------
  44. # Procedure to return the uid of a file.
  45.  
  46. proc GetUID {file} {
  47.     file stat $file stat
  48.     return $stat(uid)
  49. }
  50.  
  51. #-----------------------------------------------------------------------------
  52. # Procedure to return the gid of a file.
  53.  
  54. proc GetGID {file} {
  55.     file stat $file stat
  56.     return $stat(gid)
  57. }
  58.  
  59. #-----------------------------------------------------------------------------
  60. # Procedure to return the uid and gid of a file.
  61.  
  62. proc GetUIDGID {file} {
  63.     file stat $file stat
  64.     return [list $stat(uid) $stat(gid)]
  65. }
  66.  
  67.  
  68. #-----------------------------------------------------------------------------
  69. # If a user does not have a group name assigned, then some tests will not work,
  70. # just blow off the tests and let the user make things right. 
  71.  
  72. if {[catch {id group}] != 0} {
  73.     echo "User '[id user]' does not have group name. Chmod tests skipped"
  74.     return
  75. }
  76.  
  77. #-----------------------------------------------------------------------------
  78. # Purge existing test files and recreate them.
  79. #
  80. proc SetUpTestFiles {} {
  81.     foreach f {CHECK.TMP CHMOD.TMP CHMOD2.TMP} {
  82.         unlink -nocomplain $f
  83.         close [open $f w]
  84.     }
  85. }
  86.  
  87. SetUpTestFiles
  88.  
  89. # Set the umask so that no bits are masked.  Some system chmods use umask
  90. # if u, g, o or a are not specified in a symbolic chmod.
  91.  
  92. umask 000
  93.  
  94. Test chmod-1.1 {chmod absolute mode tests} {
  95.     chmod 0000 CHMOD.TMP
  96.     chmod 0101 CHMOD.TMP
  97.     GetMode    CHMOD.TMP
  98. } 0 {101}
  99.  
  100. Test chmod-1.2 {chmod absolute mode tests} {
  101.     chmod 0000 CHMOD.TMP
  102.     chmod 0010 CHMOD.TMP
  103.     GetMode    CHMOD.TMP
  104. } 0 {10}
  105.  
  106. Test chmod-1.3 {chmod absolute mode tests} {
  107.     chmod 0000 CHMOD.TMP
  108.     chmod 0777 CHMOD.TMP
  109.     GetMode    CHMOD.TMP
  110. } 0 {777}
  111.  
  112. Test chmod-1.4 {chmod absolute mode tests} {
  113.     chmod 0000 CHMOD.TMP
  114.     chmod 0666 CHMOD.TMP
  115.     GetMode    CHMOD.TMP
  116. } 0 {666}
  117.  
  118. Test chmod-1.5 {chmod absolute mode tests} {
  119.     chmod 0000 CHMOD.TMP
  120.     chmod 0705 CHMOD.TMP
  121.     GetMode    CHMOD.TMP
  122. } 0 {705}
  123.  
  124. Test chmod-1.7 {chmod absolute mode tests} {
  125.     chmod  0000 CHMOD.TMP
  126.     chmod 04111 CHMOD.TMP
  127.     GetMode     CHMOD.TMP
  128. } 0 {4111}
  129.  
  130. Test chmod-2.1 {chmod absolute integer mode tests} {
  131.     chmod   0 {CHMOD.TMP CHMOD2.TMP}
  132.     chmod  65 {CHMOD.TMP CHMOD2.TMP}
  133.     list [GetMode CHMOD.TMP] [GetMode CHMOD2.TMP]
  134. } 0 {101 101}
  135.  
  136. Test chmod-2.2 {chmod absolute integer mode tests} {
  137.     chmod 0 {CHMOD.TMP CHMOD2.TMP}
  138.     chmod 8 {CHMOD.TMP CHMOD2.TMP}
  139.     list [GetMode CHMOD.TMP] [GetMode CHMOD2.TMP]
  140. } 0 {10 10}
  141.  
  142. Test chmod-2.3 {chmod absolute integer mode tests} {
  143.     chmod   0 {CHMOD.TMP CHMOD2.TMP}
  144.     chmod 511 {CHMOD.TMP CHMOD2.TMP}
  145.     list [GetMode CHMOD.TMP] [GetMode CHMOD2.TMP]
  146. } 0 {777 777}
  147.  
  148. Test chmod-2.4 {chmod absolute integer mode tests} {
  149.     chmod   0 {CHMOD.TMP CHMOD2.TMP}
  150.     chmod 438 {CHMOD.TMP CHMOD2.TMP}
  151.     list [GetMode CHMOD.TMP] [GetMode CHMOD2.TMP]
  152. } 0 {666 666}
  153.  
  154. Test chmod-2.5 {chmod absolute integer mode tests} {
  155.     chmod   0 {CHMOD.TMP CHMOD2.TMP}
  156.     chmod 453 {CHMOD.TMP  CHMOD2.TMP}
  157.     list [GetMode CHMOD.TMP] [GetMode CHMOD2.TMP]
  158. } 0 {705 705}
  159.  
  160. Test chmod-2.6 {chmod absolute integer mode tests} {
  161.     chmod 0    CHMOD.TMP
  162.     chmod 2121 CHMOD.TMP
  163.     GetMode    CHMOD.TMP
  164. } 0 {4111}
  165.  
  166. # Test symbolic mode.
  167.  
  168. Test chmod-3.1 {chmod symbolic mode tests} {
  169.     chmod 000 CHMOD.TMP
  170.     chmod +r  CHMOD.TMP
  171.     GetMode   CHMOD.TMP
  172. } 0 {444}
  173.  
  174. Test chmod-3.2 {chmod symbolic mode tests} {
  175.     chmod 000 CHMOD.TMP
  176.     chmod +r  CHMOD.TMP
  177.     chmod +w  CHMOD.TMP
  178.     GetMode   CHMOD.TMP
  179. } 0 {666}
  180.  
  181. Test chmod-3.3 {chmod symbolic mode tests} {
  182.     chmod 000 CHMOD.TMP
  183.     chmod +r  CHMOD.TMP
  184.     chmod +w  CHMOD.TMP
  185.     chmod +x  CHMOD.TMP
  186.     GetMode   CHMOD.TMP
  187. } 0 {777}
  188.  
  189. Test chmod-3.4 {chmod symbolic mode tests} {
  190.     chmod 000 CHMOD.TMP
  191.     chmod +r  CHMOD.TMP
  192.     chmod +w  CHMOD.TMP
  193.     chmod +x  CHMOD.TMP
  194.     chmod -r  CHMOD.TMP
  195.     GetMode   CHMOD.TMP
  196. } 0 {333}
  197.  
  198. Test chmod-3.5 {chmod symbolic mode tests} {
  199.     chmod 000 CHMOD.TMP
  200.     chmod +r  CHMOD.TMP
  201.     chmod +w  CHMOD.TMP
  202.     chmod +x  CHMOD.TMP
  203.     chmod -r  CHMOD.TMP
  204.     chmod -w  CHMOD.TMP
  205.     GetMode   CHMOD.TMP
  206. } 0 {111}
  207.  
  208. Test chmod-3.6 {chmod symbolic mode tests} {
  209.     chmod 000 {CHMOD.TMP CHMOD2.TMP}
  210.     chmod +r  {CHMOD.TMP CHMOD2.TMP}
  211.     chmod +w  {CHMOD.TMP CHMOD2.TMP}
  212.     chmod +x  {CHMOD.TMP CHMOD2.TMP}
  213.     chmod -r  {CHMOD.TMP CHMOD2.TMP}
  214.     chmod -w  {CHMOD.TMP CHMOD2.TMP}
  215.     chmod -x  {CHMOD.TMP  CHMOD2.TMP}
  216.     list [GetMode CHMOD.TMP] [GetMode CHMOD2.TMP]
  217. } 0 {0 0}
  218.  
  219. Test chmod-3.7 {chmod symbolic mode tests} {
  220.     chmod 000     CHMOD.TMP
  221.     chmod u+x,g+x CHMOD.TMP
  222.     GetMode       CHMOD.TMP
  223. } 0 {110}
  224.  
  225. Test chmod-3.8 {chmod symbolic mode tests} {
  226.     chmod 000     {CHMOD.TMP CHMOD2.TMP}
  227.     chmod u+x,g+x {CHMOD.TMP CHMOD2.TMP}
  228.     chmod u-x,g-x {CHMOD.TMP CHMOD2.TMP}
  229.     list [GetMode CHMOD.TMP] [GetMode CHMOD2.TMP]
  230. } 0 {0 0}
  231.  
  232. # Can't +s on some systems
  233.  
  234. if [CheckChmod "ugo+x,ug+s" "---s--s--x"] {
  235.     Test chmod-3.9 {chmod symbolic mode tests} {
  236.         chmod 000        CHMOD.TMP
  237.         chmod ugo+x,ug+s CHMOD.TMP
  238.         GetMode          CHMOD.TMP
  239.     } 0 {6111}
  240. }
  241.  
  242. Test chmod-3.10 {chmod symbolic mode tests} {
  243.     chmod 000   CHMOD.TMP
  244.     chmod a+rwx CHMOD.TMP
  245.     GetMode     CHMOD.TMP
  246. } 0 {777}
  247.  
  248. Test chmod-3.11 {chmod symbolic mode tests} {
  249.     chmod 000   CHMOD.TMP
  250.     chmod a+rwx CHMOD.TMP
  251.     chmod a-rw  CHMOD.TMP
  252.     GetMode     CHMOD.TMP
  253. } 0 {111}
  254.  
  255. Test chmod-3.12 {chmod symbolic mode tests} {
  256.     chmod 000   CHMOD.TMP
  257.     chmod a=rwx CHMOD.TMP
  258.     GetMode     CHMOD.TMP
  259. } 0 {777}
  260.  
  261. Test chmod-3.13 {chmod symbolic mode tests} {
  262.     chmod 000         CHMOD.TMP
  263.     chmod u=rwx,go=rx CHMOD.TMP
  264.     GetMode           CHMOD.TMP
  265. } 0 {755}
  266.  
  267. Test chmod-3.14 {chmod symbolic mode tests} {
  268.     chmod 000 CHMOD.TMP
  269.     chmod u+t CHMOD.TMP
  270.     set mode [GetMode CHMOD.TMP]
  271.     expr "($mode == 0) || ($mode == 1000)"
  272. } 0 {1}
  273.  
  274. # +t is dificult to test if not root, just make sure it execute and hope
  275. # for the best.
  276.  
  277. Test chmod-3.14 {chmod symbolic mode tests} {
  278.     chmod 000 CHMOD.TMP
  279.     chmod u+t CHMOD.TMP
  280. } 0 {}
  281.  
  282. Test chmod-3.15 {chmod symbolic mode tests} {
  283.     chmod 000   CHMOD.TMP
  284.     chmod u+t   CHMOD.TMP
  285.     chmod u-t   CHMOD.TMP
  286. } 0 {}
  287.  
  288. Test chmod-3.16 {chmod symbolic mode tests} {
  289.     chmod 000         CHMOD.TMP
  290.     chmod a+rwx       CHMOD.TMP
  291.     chmod u-r,g-w,o-x CHMOD.TMP
  292.     GetMode           CHMOD.TMP
  293. } 0 {356}
  294.  
  295. Test chmod-4.1 {chmod error tests} {
  296.     chmod +z CHMOD.TMP
  297. } 1 {invalid file mode "+z"}
  298.  
  299. Test chmod-4.2 {chmod error tests} {
  300.     chmod
  301. } 1 {wrong # args: chmod mode filelist}
  302.  
  303. # chown and chgrp tests
  304.  
  305. #
  306. # Some machines have problems with changing group ids on files (even to your
  307. # own) if you are not root. If thats the case, bail out here.
  308. #
  309.  
  310. SetUpTestFiles
  311.  
  312.  
  313. if {[catch {chgrp [id groupid] CHMOD.TMP} msg ] != 0} {
  314.     puts stderr "*************************************************************"
  315.     puts stderr "Can't do chgrp even when current and new group id are"
  316.     puts stderr "ours.  Maybe a strange system."
  317.     puts stderr "    $msg"
  318.     puts stderr "*************************************************************"
  319.     unlink {CHECK.TMP CHMOD.TMP CHMOD2.TMP}
  320.     return
  321. }
  322.  
  323. set myUID [id userid]
  324. set myGID [id groupid]
  325.  
  326. Test chmod-5.1 {chown tests} {
  327.     SetUpTestFiles
  328.     chown [id user] {CHMOD.TMP CHMOD2.TMP}
  329.     list [GetUID CHMOD.TMP] [GetUID CHMOD2.TMP]
  330. } 0 [list $myUID $myUID]
  331.  
  332. Test chmod-5.2 {chown tests} {
  333.     chown [id userid] {CHMOD.TMP CHMOD2.TMP}
  334.     list [GetUID CHMOD.TMP] [GetUID CHMOD2.TMP]
  335. } 0 [list $myUID $myUID]
  336.  
  337. Test chmod-5.3 {chown tests} {
  338.     chown [list [id userid] [id groupid]] {CHMOD.TMP CHMOD2.TMP}
  339.     list [GetUIDGID CHMOD.TMP] [GetUIDGID CHMOD2.TMP]
  340. } 0 [list [list $myUID $myGID] [list $myUID $myGID]]
  341.  
  342. Test chmod-5.4 {chown tests} {
  343.     chown [list [id user] [id group]] {CHMOD.TMP CHMOD2.TMP}
  344.     list [GetUIDGID CHMOD.TMP] [GetUIDGID CHMOD2.TMP]
  345. } 0 [list [list $myUID $myGID] [list $myUID $myGID]]
  346.  
  347. Test chmod-5.5 {chown tests} {
  348.     chown [list [id user] [id group]] {CHMOD.TMP CHMOD2.TMP}
  349.     list [GetUIDGID CHMOD.TMP] [GetUIDGID CHMOD2.TMP]
  350. } 0 [list [list $myUID $myGID] [list $myUID $myGID]]
  351.  
  352. SetUpTestFiles
  353.  
  354. Test chmod-6.1 {chown error tests} {
  355.     chown XXXXXXXXX CHMOD.TMP
  356. } 1 {unknown user id: XXXXXXXXX}
  357.  
  358. Test chmod-6.2 {chown error tests} {
  359.     chown [list XXXXXXXXX [id groupid]] CHMOD.TMP
  360. } 1 {unknown user id: XXXXXXXXX}
  361.  
  362. Test chmod-6.3 {chown error tests} {
  363.     chown [list [id user] XXXXXXXXX] CHMOD.TMP
  364. } 1 {unknown group id: XXXXXXXXX}
  365.  
  366. Test chmod-6.4 {chown error tests} {
  367.     chown {XXXXXXXXX YYYY} CHMOD.TMP
  368. } 1 {unknown user id: XXXXXXXXX}
  369.  
  370. Test chmod-6.5 {chown error tests} {
  371.     chown
  372. } 1 {wrong # args: chown owner|{owner group} filelist}
  373.  
  374. Test chmod-7.1 {chgrp tests} {
  375.     chgrp [id group]  {CHMOD.TMP CHMOD2.TMP}
  376.     list [GetGID CHMOD.TMP] [GetGID CHMOD2.TMP]
  377. } 0 [list $myGID $myGID]
  378.  
  379. Test chmod-7.2 {chgrp tests} {
  380.     chgrp [id groupid] {CHMOD.TMP CHMOD2.TMP}
  381.     list [GetGID CHMOD.TMP] [GetGID CHMOD2.TMP]
  382. } 0 [list $myGID $myGID]
  383.  
  384. Test chmod-8.1 {chgrp error tests} {
  385.     chgrp
  386. } 1 {wrong # args: chgrp group filelist}
  387.  
  388. Test chmod-8.2 {chgrp error tests} {
  389.     chgrp XXXXXXXXX CHMOD.TMP
  390. } 1 {unknown group id: XXXXXXXXX}
  391.  
  392. unlink {CHECK.TMP CHMOD.TMP CHMOD2.TMP}
  393.  
  394.